﻿using CommDll;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
namespace InstitutionAssitant
{
    class ModelGoodsCodeCheck
    {
        public static string strSupplierCode;
        public static string strSupplierName;
        public static string strID0;        
        public static string strCode0;
        public static string strUnit0;
        public static string strUnitMinForm0;
        public static double dWarehouseRatio0;
        public static string strWarehouseCode0;
        public static string strWarehouseName0;
        public static string strName0;
        public static string strForm0;
        public static string strRegistrationCode0;
        public static string strSpec0;
        public static string strManufacturer0;
        public static string strKind0;
        public static string strPrice0;
        public static string strCode1;
        public static double dPrice1;   
        public static string strUnit1;
        public static string strName1;
        public static string strForm1;
        public static string strRegistrationCode1;
        public static string strSpec1;
        public static string strManufacturer1;
        public static string strWarehouseCode1;
        public static string strWarehouseName1;
        public static int _saveInfo()
        {
            string strSql = "";
            // 删除原数据
            strSql = string.Format("Delete from T_Base_GoodsCodeCheck where InstitutionCode = '{0}' and SupplierCode = '{1}' and CodeSupplier = '{2}'",AppInfo.m_UserInfo.m_strDeptCode, strSupplierCode, strCode0);
            int nResult = DbHelper.DBAccess[0]._execSql(strSql);
            strSql = string.Format("Delete from T_Base_GoodsCodeCheck where InstitutionCode = '{0}' and SupplierCode = '{1}' and CodeSupplier = '{2}'", AppInfo.m_UserInfo.m_strDeptCode, strSupplierCode, strCode0);
            if (!strCode1.Equals(""))
            {
                // 插入新数据
                strSql = string.Format("Insert into T_Base_GoodsCodeCheck(SupplierCode,SupplierName,InstitutionCode,InstitutionName,CodeSupplier,Code,Name,RegistrationCode,Form,Spec,Unit,Manufacturer,WarehouseRatio,WarehouseCode,WarehouseName,CheckCodeStatus,UpdateTime,CheckCodeTime) Values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}',1,getdate(),getdate())"
                    , strSupplierCode, strSupplierName, AppInfo.m_UserInfo.m_strDeptCode, AppInfo.m_UserInfo.m_strDeptName, strCode0,strCode1
                    , strName0, strRegistrationCode0.Replace("'", "''"), strForm0, strSpec0.Replace("'", "''"), strUnit0, strManufacturer0.Replace("'", "''")
                    , dWarehouseRatio0, strWarehouseCode0.Replace("'", "''"), strWarehouseName0.Replace("'", "''"));
                nResult = DbHelper.DBAccess[0]._execSql(strSql);
            };
            // 更新配送数据
            strSql = "exec P_InitDelivery 'GoodsCodeCheck','{0},{1},{2},{3}'";
            strSql = string.Format(strSql, AppInfo.m_UserInfo.m_strDeptCode, strSupplierCode, strCode0, strCode1);
            nResult = DbHelper.DBAccess[0]._execSql(strSql);
            return nResult;
        }
    }

    class ModelSupplierCodeCheck
    {
        public static string strID0;
        public static string strCode0;
        public static string strName0;
        public static string strLinkman0;
        public static string strSupplierCode;
        
        public static string strCode1;
        public static string strName1;
        public static string strLinkman1;
        public static int _saveInfo()
        {
            string strSql = string.Format("Update T_Base_SupplierCodeCheck set SupplierCode = '{1}', SupplierName = '{2}', Linkman = '{3}', CheckCodeStatus=1,CheckCodeTime= getDate() where ID = {0}", strID0, strCode1, strName1, strLinkman0);
            if (IsExist())
            {
                if (strCode1.Equals("暂不对码"))
                {
                    strSql = string.Format("Update T_Base_SupplierCodeCheck set SupplierCode = '{1}', SupplierName = '',Linkman = '',CheckCodeStatus=-1,CheckCodeTime= getDate() where ID = {0}", strID0, strCode1);
                }
            }
            else
            {
                strSql = string.Format("insert into T_Base_SupplierCodeCheck(InstitutionCode,InstitutionName,Code,Name,SupplierCode,SupplierName,Linkman,CheckCodeStatus,CheckCodeTime) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}',1,getDate())", AppInfo.m_UserInfo.m_strDeptCode, AppInfo.m_UserInfo.m_strDeptName, strCode0, strName0, strCode1, strName1, strLinkman0);
            }
            int nResult = DbHelper.DBAccess[0]._execSql(strSql);

            // 更新配送数据
            // 更新原数据新数据
            strSql = "Update T_DeliveryDetail set SupplierCode_His = '',SupplierCode_Name = '' where InstitutionCode = '{0}' and SupplierCode_His = '{1}'";
            strSql = string.Format(strSql, AppInfo.m_UserInfo.m_strDeptCode, strCode0);
            nResult = DbHelper.DBAccess[0]._execSql(strSql);
            // 更新原数据
            strSql = "exec P_InitDelivery 'SupplierCodeCheck','{0},{1},'";
            strSql = string.Format(strSql, AppInfo.m_UserInfo.m_strDeptCode, strCode1);
            nResult = DbHelper.DBAccess[0]._execSql(strSql);

            return nResult;
        }
        private static bool IsExist()
        {
            bool result = false;
            string strSql = string.Format("select count(ID) from T_Base_SupplierCodeCheck where SupplierCode = '{0}' and SupplierName = '{1}' and Linkman = '{2}'", strCode1, strName1, strLinkman0);
            int nResult = DbHelper.DBAccess[0]._execSql(strSql);
            if (nResult > 0)
                result = true;
            return result;
        }
    }
}
